#!/usr/bin/python3
import requests,re,warnings,json,random

warnings.filterwarnings("ignore")
userAgent ="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"


"""
注册
"""
def register():
    emailAdress = "exfs41u4@nqmo.com"
    findEmailCode(emailAdress=emailAdress)

"""
查询邮件验证码，入参邮箱地址
"""
def findEmailCode(emailAdress):
    #获取token
    firstUrl = "https://mail.td/zh/mail/"+emailAdress
    session = requests.session()
    response = session.get(firstUrl,verify=False)
    setCookie = response.headers["Set-Cookie"]
    authTokenNotStart = re.split("auth_token=",setCookie)[1]
    authToken = re.split("; Path=",authTokenNotStart)[0]
    print("authToken=",authToken)
    #获取邮件列表
    response = session.get(url="https://mail.td/api/api/v1/mailbox/"+emailAdress,
                           headers={"Authorization": "bearer "+authToken},
                           verify=False)
    response.encoding = response.apparent_encoding
    print("邮件列表="+response.text)
    newMail = json.loads(response.text)[0]
    mailId = newMail["id"]
    mailSubject = newMail["subject"]
    print("邮件id={},邮件标题={}".format(mailId,mailSubject))
    #获取邮件详细信息
    response = session.get(url="https://mail.td/api/api/v1/mailbox/"+emailAdress+"/"+mailId,
                           headers={"Authorization": "bearer "+authToken},
                           verify=False)
    response.encoding = response.apparent_encoding
    newMail = json.loads(response.text)
    newMailHtml = newMail["body"]["html"]
    print("新邮件内容="+newMailHtml)